﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using AutoCorrelatorGUI.Model;
using AutoCorrelatorGUI.Properties;
using AutoCorrelatorGUI.Utility;
using AutoCorrelatorGUI.ViewModel.Messages;
using Common.Utility;
using Common.ViewModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using OxyPlot;
using OxyPlot.Series;

namespace AutoCorrelatorGUI.ViewModel
{
   /// <summary>
   /// This class contains properties that a View can data bind to.
   /// <para>
   /// See http://www.galasoft.ch/mvvm
   /// </para>
   /// </summary>
   public class OutputDisplayViewModel : ViewModelBase
   {
      /// <summary>
      /// Initializes a new instance of the MvvmViewModel1 class.
      /// </summary>
      public OutputDisplayViewModel()
      {
         Messenger.Default.Register<FullRefreshMessage>(this, OnFullRefresh);

         Style headerColorStyle = new Style(typeof(TextBlock));
         headerColorStyle.Setters.Add(new Setter(TextBlock.BackgroundProperty, new Binding("Color") { Converter = new ColorToBrushConverter() }));

         OutputDataColumnCollection = new ObservableCollection<DataGridColumn>();
         OutputDataColumnCollection.Add(new DataGridTextColumn() { Header = "ID", Binding = new Binding("Title"), ElementStyle = headerColorStyle });

         //Request any existing data
         Messenger.Default.Send(new RequestDataMessage(OnFullRefresh));
      }

      private void OnFullRefresh(FullRefreshMessage message)
      {
         InternalWellData = message.DataView.SourceCollection as MultiSelectBindingList<WellVM>;
         InternalWellDataView = message.DataView;

         //Find the min and max range
         int outRangeMin = Int32.MaxValue;
         int outRangeMax = Int32.MinValue;

         foreach (WellVM well in InternalWellData)
         {
            foreach (var point in well.Output.Data)
            {
               int xInt = (int)Math.Round(point.X);

               outRangeMin = Math.Min(outRangeMin, xInt);
               outRangeMax = Math.Max(outRangeMax, xInt);
            }
         }

         //just remove all of the columns except for the id column
         while (OutputDataColumnCollection.Count > 1)
         {
            OutputDataColumnCollection.RemoveAt(OutputDataColumnCollection.Count - 1);
         }

         for (int i = outRangeMin; i <= outRangeMax; i++)
         {
            var col = new DataGridTextColumn() { Header = i.ToString(), Binding = new Binding("Output.DataStore[" + i.ToString() + "]") { StringFormat = "G8" }, CanUserSort = true, SortMemberPath = "Output.DataStore[" + i.ToString() + "]" };
            CustomSort.SetCustomSorter(col, new WellBaseComparer());
            OutputDataColumnCollection.Add(col);
         }
      }

      private MultiSelectBindingList<WellVM> _InternalWellData = null;

      public MultiSelectBindingList<WellVM> InternalWellData
      {
         get { return _InternalWellData; }
         set
         {
            if (_InternalWellData == value)
            {
               return;
            }

            _InternalWellData = value;
            RaisePropertyChanged("InternalWellData");
         }
      }

      private ListCollectionView _InternalWellDataView = null;

      public ListCollectionView InternalWellDataView
      {
         get
         {
            return _InternalWellDataView;
         }

         set
         {
            if (_InternalWellDataView == value)
            {
               return;
            }

            _InternalWellDataView = value;
            RaisePropertyChanged("InternalWellDataView");
         }
      }

      public ObservableCollection<DataGridColumn> OutputDataColumnCollection
      {
         get;
         private set;
      }
   }
}
